package com.goldman.aplt.service.droplist;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.goldman.aplt.base.type.BaseEnumType;
import com.goldman.aplt.entity.droplist.AppDropList;
import com.goldman.aplt.repo.droplist.AppDropListMapper;
import com.goldman.aplt.util.CommUtil;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.goldman.aplt.base.constant.ApConstants.ENUM_SYMBOL;

@CacheConfig(cacheNames = "${systemId}:AppDropListService")
@Service
public class AppDropListService extends ServiceImpl<AppDropListMapper, AppDropList> {
    @Cacheable
    public List<AppDropList> getListByType(String dropType) {
        CommUtil.fieldNotNull(dropType, "dropType");
        QueryWrapper<AppDropList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("drop_list_type",dropType);
        queryWrapper.eq("enable_ind", BaseEnumType.E_YESORNO.YES.getValue());
        queryWrapper.orderByAsc("data_sort");
        return list(queryWrapper);
    }
    @Cacheable
    public List<AppDropList> getListBySuper(String superType, String superValue) {
        CommUtil.fieldNotNull(superType, "superType");
        CommUtil.fieldNotNull(superValue, "superValue");
        QueryWrapper<AppDropList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("super_drop_list_type",superType);
        queryWrapper.eq("super_drop_list_value",superValue);
        queryWrapper.eq("enable_ind", BaseEnumType.E_YESORNO.YES.getValue());
        queryWrapper.orderByAsc("data_sort");
        return list(queryWrapper);
    }
//    @Cacheable
    public boolean exists(String dropType, String value) {
        CommUtil.fieldNotNull(dropType, "dropType");
        CommUtil.fieldNotNull(dropType, "value");
        return lambdaQuery().eq(AppDropList::getDropListType, dropType)
                .eq(AppDropList::getDropListValue, value).one() != null;
    }
//    @Cacheable
    public String toAppValue(String dropType, String value, String superValue) {
        CommUtil.fieldNotNull(dropType, "dropType");
        CommUtil.fieldNotNull(dropType, "value");

        AppDropList drop;
        if(CommUtil.isNull(superValue)) {
            drop = lambdaQuery().eq(AppDropList::getDropListType, dropType)
                    .eq(AppDropList::getDropListValue, value).one();
        } else {
            drop = lambdaQuery().eq(AppDropList::getDropListType, dropType)
                    .eq(AppDropList::getDropListValue, value)
                    .eq(AppDropList::getSuperDropListValue, superValue).one();
        }

        String appValue = "";
        if(drop != null) {
            appValue = drop.getDropListValue() + ENUM_SYMBOL + drop.getDropListDesc();
        }

        return appValue;
    }

}
